# urllib 的Request类可以用于构建一个请求

from urllib.request import Request
import urllib.request as ur

ua_header = {'User-Agent':'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0;'} # 模拟IE9
myreq = Request('http://www.baidu.com', headers=ua_header)
myreq.add_header('Connection', 'keep-alive')                # 添加更多的Header信息

print(myreq.get_header('User-agent'))                       # 可以反过来获取request的header

response = ur.urlopen(myreq)
html = response.read().decode('utf8')
print(html)


